package cn.itcast.web.controller.packing;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.itcast.domain.packing.Invoice;
import cn.itcast.domain.packing.ShippingOrder;
import cn.itcast.service.packing.InvoiceService;
import cn.itcast.service.packing.ShippingOrderService;
import cn.itcast.util.DownloadUtils;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.sql.Struct;
import java.util.Map;

@Controller
@RequestMapping(value = "/cargo/invoice", name = "发票什么")
public class InvoiceController extends BaseController {

    @Reference
    private ShippingOrderService shippingOrderService;

    @Reference
    private InvoiceService invoiceService;

    @RequestMapping(value = "/toInvoice", name = "去财务玩")
    public String toInvoice(String shippingOrderId) {

        request.setAttribute("shippingOrderId", shippingOrderId);

        return "packing/invoice/invoice-toInvoice";
    }

    @RequestMapping(value = "/edit", name = "新增/修改")
    public String edit(Invoice invoice, String shippingOrderId) {

        if (StrUtil.hasEmpty(invoice.getInvoiceId())) {
            invoice.setInvoiceId(shippingOrderId);
            invoice.setState(0);
            invoiceService.save(invoice);
        } else {
            invoiceService.update(invoice);
        }
        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping(value = "list", name = "发票列表")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize) {

        PageInfo page = invoiceService.findByPage(pageNum, pageSize, null);
        request.setAttribute("page", page);
        return "packing/invoice/invoice-list";
    }

    @RequestMapping(value = "/toUpdate", name = "去修改页面")
    public String toUpdate(String invoiceId) {
        Invoice invoice = invoiceService.findById(invoiceId);
        request.setAttribute("invoice", invoice);
        return "/packing/invoice/invoice-update";
    }

    @RequestMapping(value = "/delete", name = "删除")
    public String delete(String invoiceId) {
        invoiceService.delete(invoiceId);
        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping(value = "/submit", name = "委托上报")
    public String submit(String invoiceId) {
        Invoice invoice = invoiceService.findById(invoiceId);
        invoice.setState(1);
        invoiceService.update(invoice);
        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping(value = "/cancel", name = "取消上报委托")
    public String cancel(String invoiceId) {
        Invoice invoice = invoiceService.findById(invoiceId);
        invoice.setState(0);
        invoiceService.update(invoice);
        return "redirect:/cargo/invoice/list.do";
    }

    @RequestMapping(value = "/toExcel", name = "下载excel")
    public void toExcel(String invoiceId) throws IOException {
        Invoice invoice = invoiceService.findById(invoiceId);
        Map<String, Object> map = BeanUtil.beanToMap(invoice, false, true);

        String realPath = session.getServletContext().getRealPath("/make/selfxls/poi_invoice.xlsx");
        TemplateExportParams templateExportParams = new TemplateExportParams(realPath);
        Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, map);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        workbook.write(byteArrayOutputStream);
        DownloadUtils.download(byteArrayOutputStream, response, "invoice.xlsx");
    }
}
